﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using CPETwit.WPFClient.Business.Service;
using CPETwit.WPFClient.Business.TwitterServiceReference;
using CPETwit.WPFClient.Command;
using CPETwit.WPFClient.Common;

namespace CPETwit.WPFClient.ViewModel
{
    public class ProfileDisplayingViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        
        private ICommand _editProfileCommand;
        private User _currentUser;
        private ICommand _followCommand;

        public ICommand EditProfileCommand
        {
            get
            {
                return _editProfileCommand ??
                       (_editProfileCommand = new RelayCommand(param => EditProfil(),
                                                               param => CanEditProfil()));
            }
        }
        public ICommand FollowCommand
        {
            get
            {
                return _followCommand ??
                    new RelayCommand(param => FollowUser(param as User),
                                     param => CanFollowUser());
            }
        }
        public User CurrentUser
        {
            get { return _currentUser; }
            private set
            {
                _currentUser = value;
                OnPropertyChanged("CurrentUser");
                OnPropertyChanged("CanDisplayAbonnementButton");
                OnPropertyChanged("CanShowEditProfileButton");
            }
        }

        public bool CanDisplayAbonnementButton
        {
            get { return CommonData.LOGGED_USER != _currentUser; }
        }
        public bool CanShowEditProfileButton
        {
            get { return CommonData.LOGGED_USER == _currentUser; }
        }

        private bool CanEditProfil()
        {
            if (CommonData.LOGGED_USER != null && _currentUser != null)
                return CommonData.LOGGED_USER.id == _currentUser.id;
            return false;
        }
        private void EditProfil()
        {
            DialogManager.ShowProfileEdition();
        }

        private void FollowUser(User user)
        {
            ServiceManager.ManageFollow(user);
            ServiceManager.RefreshCurrentUser();
            CurrentUser = ServiceManager.RefreshUser(user);
        }
        private bool CanFollowUser()
        {
            return CommonData.LOGGED_USER != null;
        }

        public ProfileDisplayingViewModel()
        {
            if (App.IsInDesignMode())
            {
                UpdateViewModel(new User
                         {
                    name = "Pierre Gillon",
                    email = "gillon.pierre69@gmail.com",
                    login = "pgillon",
                    password = "Bonjour",
                    web = "www.google.fr",
                    description = "Je m'appelle henri, j'aimerais réussir ma vie...",
                    sex = "M",
                    birthdate = DateTime.Now,
                    avatar = null
                });
            }
            else
            {
                if (CommonData.LOGGED_USER != null)
                    UpdateViewModel(CommonData.LOGGED_USER);
            }
        }

        public void UpdateViewModel(User user)
        {
            CurrentUser = user;
        }
    }
}
